package com.tianda.his.modules.erp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianda.his.commons.utils.LoginUtils;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.erp.dto.ProviderDto;
import com.tianda.his.modules.erp.dto.PruducerDto;
import com.tianda.his.modules.erp.entity.StockProvider;
import com.tianda.his.modules.erp.service.StockProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 生产厂家管理
 */
@RestController
public class StockProviderController {

    @Autowired
    private StockProviderService stockProviderService;

    /**
     * 分页查询
     * @param providerDto
     * @return
     */
    @GetMapping("/erp/provider/listProviderForPage")
    public AjaxResult listProviderForPage(ProviderDto providerDto) {
        Page<StockProvider> pageResult = stockProviderService.listProviderForPage(providerDto);
        return AjaxResult.success(pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 新增供应商
     * @param stockProvider
     * @return
     */
    @PostMapping("/erp/provider/addProvider")
    public AjaxResult addProvider(StockProvider stockProvider) {
        stockProvider.setCreateTime(new Date());
        stockProvider.setCreateBy(LoginUtils.getLoginUser().getUserName());

        stockProviderService.save(stockProvider);
        return AjaxResult.success();

    }

    /**
     * 修改供应商
     * @param stockProvider
     * @return
     */
    @PutMapping("/erp/provider/updateProvider")
    public AjaxResult updateProvider(StockProvider stockProvider) {
        stockProvider.setUpdateTime(new Date());
        stockProvider.setUpdateBy(LoginUtils.getLoginUser().getUserName());

        stockProviderService.updateById(stockProvider);
        return AjaxResult.success();
    }

    /**
     * 根据id查询
     * @param providerId
     * @return
     */
    @GetMapping("/erp/provider/getProviderById/{providerId}")
    public AjaxResult getProviderById(@PathVariable("providerId") Long providerId) {
        return AjaxResult.success(stockProviderService.getById(providerId));
    }

    /**
     * 删除供应商
     * @param providerId
     * @return
     */
    @DeleteMapping("/erp/provider/deleteProviderByIds/{providerId}")
    public AjaxResult deleteProviderById(@PathVariable("providerId") Long providerId) {
        stockProviderService.removeById(providerId);
        return AjaxResult.success();
    }

    /**
     * 查询所有供应商
     * @return
     */
    @GetMapping("/erp/provider/selectAllProvider")
    public AjaxResult selectAllProvider() {
        return AjaxResult.success(stockProviderService.list());
    }


}
